#!/bin/bash

# 检查并取消内核包的锁定状态
function unhold_kernel_packages() {
    local held_packages
    held_packages=$(sudo apt-mark showhold)

    # 匹配所有被锁定的linux-image-deepin开头的包
    local linux_image_packages
    linux_image_packages=$(echo "$held_packages" | grep "linux-image-deepin")

    if [ -n "$linux_image_packages" ]; then
        echo "发现以下被锁定的内核包:"
        echo "$linux_image_packages"

        # 遍历每个被锁定的内核包并取消锁定状态
        while read -r package; do
            echo "取消 ${package} 的锁定状态..."
            sudo apt-mark unhold "$package"
            if [ $? -eq 0 ]; then
                echo "成功取消 ${package} 的锁定状态。"
            else
                echo "取消 ${package} 的锁定状态失败。"
            fi
        done <<< "$linux_image_packages"
    else
        echo "未发现被锁定的内核包。"
    fi
}

# pkg_update 更新影响系统更新的组件.
# 当前系统组件的版本过低，存在bug，会影响系统升级，需要先更新下述组件：
# - deepin-immutable-ctl
# - dde-control-center
# - deepin-update-ui
# - deepin-system-fixpkg
# - deepin-system-update
function pkg_update() {
    echo "将要更新以下软件包："
    echo "\tdeepin-immutable-ctl"
    echo "\tdde-control-center"
    echo "\tdeepin-update-ui"
    echo "\tdeepin-system-fixpkg"
    echo "\tdeepin-system-update"

    sudo apt update
    sudo apt install deepin-immutable-ctl dde-control-center deepin-update-ui \
         deepin-system-fixpkg deepin-system-update -y

    if [ $? -eq 0 ]; then
        echo "软件更新成功。"
    else
        echo "软件更新失败，请根据错误信息进行修复，然后重试。"
        exit 1
    fi
}

# list_snapshot 解析deepin-immutable-ctl admin status的输出，得到快照ExtCommitIdIdx列表。
# deepin-immutable-ctl admin status输出的内容如下：
# *  (index: 0)
#    ExtCommitIdIdx: c2bdc82f8609faf81a5fb40c6ec58507f5b1613b454c158dd1ece9cc9c6c0407.0
#    BaseCommitIdIdx: 29a3bf6e2bf2d0e5c494fb01900c7ee1bd4c12dd1f4d00ef6c95a4d978aa296d.0
#
#    (index: )
#    ExtCommitIdIdx: c2bdc82f8609faf81a5fb40c6ec58507f5b1613b454c158dd1ece9cc9c6c0407.0
#    BaseCommitIdIdx: 29a3bf6e2bf2d0e5c494fb01900c7ee1bd4c12dd1f4d00ef6c95a4d978aa296d.0
#
#
function list_snapshot() {
    local output
    output=$(deepin-immutable-ctl admin status)

    # 使用grep和awk提取ExtCommitIdIdx的值
    grep "ExtCommitIdIdx:" <<< "$output" | awk '{print $2}'
}

# cleanup_snapshot 根据idx清理快照
function cleanup_snapshot() {
    local idx=$1

    sudo deepin-immutable-ctl admin undeploy ${idx}

    if [ $? -eq 0 ]; then
        echo "快照清理成功。"
    else
        echo "快照清理失败，请根据错误信息进行修复，然后重试。"
        exit 1
    fi
}

function main() {
    # 取消内核包的锁定状态
    unhold_kernel_packages

    # 更新组件
    pkg_update
    
    # 获取snapshot列表
    local snapshot_list
    snapshot_list=$(list_snapshot)

    # 设置保留快照数量，默认保留2个快照
    local keep_count=2
    
    # 将快照列表按行分割成数组
    local -a snapshots=($(echo "$snapshot_list"))

    # 获取快照总数
    local snapshot_count=${#snapshots[@]}

    # 计算需要清理的快照数量
    local cleanup_count=$((snapshot_count - keep_count))

    if [ "$cleanup_count" -gt 0 ]; then
        echo "需要清理 $cleanup_count 个快照。"

        # 清理快照
        for ((i=0; i<cleanup_count; i++)); do
            cleanup_snapshot "$((keep_count+i))"
        done
    else
        echo "无需清理快照，当前快照数量小于等于保留数量（$keep_count）。"
    fi

    echo "操作完成，请重启后到控制中心执行更新。"
}

main
